<template>
	<view class="login">
		<view class="backView">
			<image class="topImg" src="https://chongwu.aitaoba.net/storage/uploads/20231118/0965d03c743303189ced18707ae69772.png"></image>
			<button class="loginButton" withCredentials="true" lang="zh_CN" open-type="getUserInfo"
				@getuserinfo="wxGetUserInfo">微信授权登录</button>
		</view>
	</view>
</template>

<script>
	import {
		wxLogin
	} from "@/api/login.js"
	import {getUserInfo} from '@/api/my.js'
	export default {
		data() {
			return {

			}
		},
		methods: {
			wxGetUserInfo(e) {
				// wx.showModal({
				// 	title: '温馨提示',
				// 	content: '亲，授权微信登录后才能正常使用小程序功能',
				// 	success(reso) {
				// 		console.log(reso)
				// 		//如果用户点击了确定按钮
				// 		if (reso.confirm) {

				// 			uni.getUserProfile({
				// 				lang: 'zh_CN',
				// 				desc: '获取你的昵称、头像、地区及性别', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，
				// 				success: (resq) => {
				// 					console.log(resq, 'resss')
				// 					uni.login({
				// 						success(res) {
				// 							console.log(res, '看看授权登录拿到的信息')
				// 							console.log(res.code) //获取到的code
				// 							let data = {
				// 								code: res.code
				// 							};

				// 							wxLogin(data).then((res) => {

				// 								if (res.code == 200) {
				// 									let token = res.data.token;
				// 									console.log(token, '看看token')
				// 									uni.setStorageSync("token", token);
				// 									uni.navigateTo({
				// 										url: "/pages/getAPhoneNumber/getAPhoneNumber"
				// 									})
				// 								}
				// 							}).catch((res) => {})
				// 						}
				// 					})
				// 				},
				// 				fail: (err) => {
				// 					console.log(err, '错误')
				// 				}
				// 			})
				// 		}
				// 	}
				// })

				uni.getUserInfo({
					provider: 'weixin',
					success: infoRes => {
				console.log(infoRes,'看看用户信息')
						this.userInfo=infoRes.userInfo; //用户信息，微信头像，昵称等等
						uni.login({
							success(res){
						// console.log(res,'看看授权登录拿到的信息')
								console.log(res.code)  //获取到的code
						let data = {code:res.code};

						wxLogin(data).then((res)=>{

							if(res.code==200){
								let token = res.data.token;
								// console.log(token,'看看token')
								uni.setStorageSync("token",token);
								
								getUserInfo({}).then(res => {
									const phone = res.data.phone;
									if(!phone){
										uni.reLaunch({
											url:"/pages/getAPhoneNumber/getAPhoneNumber"
										})
									}else{
										uni.navigateBack({
											delta:1
										})
									}
								})
								
							}
						}).catch((res)=>{})
							}
						})			
					},
					fail(res) {
						uni.showToast({
							title:'登录授权失败',
							icon:'none',
						})
					}
				});

			},



		}
	}
</script>

<style lang="scss" scoped>
	.login {
		width: 100%;
		display: flex;
		flex-direction: column;
	}

	.backView {
		width: 100%;

		height: 100vh;
		background: linear-gradient(180deg, #FAE24E 0%, rgba(255, 255, 255, 0) 100%);
		display: flex;
		flex-direction: column;
		align-items: center;
		padding-top: 147rpx;
		box-sizing: border-box;

	}

	.topImg {
		width: 361rpx;
		height: 361rpx;
		// background: gold;
	}

	.loginButton {
		width: 630rpx;
		height: 100rpx;
		background: #FFCE10;
		box-shadow: 0rpx 6rpx 10rpx 1rpx rgba(0, 0, 0, 0.1608);
		border-radius: 50rpx;
		font-size: 36rpx;
		font-family: PingFang SC-Heavy, PingFang SC;
		font-weight: 800;
		color: #2E2908;
		text-align: center;
		line-height: 100rpx;
		margin-top: 188rpx;
	}
</style>
